package action.info;

import javax.servlet.http.HttpServletRequest;

import net.fckeditor.FCKeditor;

import org.apache.struts2.interceptor.ServletRequestAware;

import service.BulletinService;

import com.opensymphony.xwork2.ActionSupport;

public class BulletinPublishAction extends ActionSupport implements ServletRequestAware {

    private static final long serialVersionUID = 8876666024069735597L;

    private HttpServletRequest request;

    private BulletinService bulletinService;

    private String editorHtml;

    private String editorInstanceName;

    private String bulletinTitle;

    private String bulletinContent;

    private String bulletinId;

    private String publishState;

    public String preparePublish() throws Exception {

        editorInstanceName = "bulletinEditor";

        FCKeditor editor = new FCKeditor(request, "bulletinEditor");
        editor.setToolbarSet("Bulletin");
        editor.setHeight("400px");

        editorHtml = editor.createHtml();

        return SUCCESS;
    }

    public String publish() throws Exception {

        if (bulletinTitle == null || "".equals(bulletinTitle)) {
            publishState = "null title";
        } else if (bulletinContent == null || "".equals(bulletinContent)) {
            publishState = "null content";
        } else {
            bulletinService.saveBulletin(bulletinTitle, bulletinContent);
            publishState = "success";
        }
        return SUCCESS;
    }

    public String rePublish() throws Exception {
        if (bulletinTitle == null || "".equals(bulletinTitle)) {
            publishState = "null title";
        } else if (bulletinContent == null || "".equals(bulletinContent)) {
            publishState = "null content";
        } else {
            bulletinService.updateBulletin(bulletinId, bulletinTitle, bulletinContent);
            publishState = "success";
        }
        return SUCCESS;
    }

    public String getEditorHtml() {
        return editorHtml;
    }

    public String getEditorInstanceName() {
        return editorInstanceName;
    }

    public void setBulletinTitle(String bulletinTitle) {
        this.bulletinTitle = bulletinTitle;
    }

    public void setBulletinContent(String bulletinContent) {
        this.bulletinContent = bulletinContent;
    }

    public void setBulletinId(String bulletinId) {
        this.bulletinId = bulletinId;
    }

    public String getPublishState() {
        return publishState;
    }

    public void setServletRequest(HttpServletRequest request) {
        this.request = request;
    }

    public void setBulletinService(BulletinService bulletinService) {
        this.bulletinService = bulletinService;
    }

}
